const Service = require('egg').Service;
const { Sequelize, Op } = require('sequelize');

class StufileService extends Service {
    // 添加用service
    async add(sfile) {
        const { ctx } = this;
        const result = await ctx.model.Stufile.create(sfile);
        return result;
    }
    // 分页查询用service
    async findList(keywords, pageIndex, pageSize) {
        const { ctx } = this;
        const result = await ctx.model.Stufile.findAndCountAll({
            where: {
                filename: {
                    [Op.like]: '%' + keywords + '%'
                }
            },
            include: [
                {
                    model: ctx.model.Student,
                    as: 'student'
                }
            ],
            offset: (pageIndex - 1) * pageSize,
            limit: pageSize
        });
        return result;
    }
    // 根据id查找对应的值
    async findById(id) {
        const { ctx } = this;
        const result = await ctx.model.Stufile.findOne({
            where: {
                id: id
            },
            include: [
                {
                    model: ctx.model.Student,
                    as: 'student'
                }
            ],
        });
        return result;
    }
    // 修改
    async modify(topic) {
        const { ctx } = this;
        const {
            id,
            canbedelete
        } = topic;
        const result = await ctx.model.Stufile.update({
            canbedelete
        }, {
            where: { id }
        });
        return result;
    }
    // 删除
    async remove(id) {
        const { ctx } = this;
        const result = await ctx.model.Stufile.destroy({
            where: { id }
        });
        return result;
    }
    // 查找学生当前阶段提交的文件(未完成)
    async findByStuCurrentId(keywords, pageIndex, pageSize, stu_id) {
        const { ctx } = this;
        const result = await ctx.model.Stufile.findAndCountAll({
            where: {
                filename: {
                    [Op.like]: '%' + keywords + '%'
                },
                stu_id,
            },
            offset: (pageIndex - 1) * pageSize,
            limit: pageSize,
            order: [
                ['createdAt', 'DESC']
            ],
        });
        return result;
    }
    // 查找学生当前阶段提交的文件(已完成)
    async findbyNeedReviewby(stu_id, need) {
        const { ctx } = this;
        const result = await ctx.model.Stufile.findAll({
            where: {
                stu_id,
                progress: {
                    [Op.in]: need,
                },
            },
            attributes: [
                'created_at',
                [Sequelize.fn('GROUP_CONCAT', Sequelize.col('id')), 'file_list_latest'],
            ],
            order: [
                ['created_at', 'DESC']
            ],
            group: 'created_at'
        });
        // 最新上传的文件的编号
        console.log('最新上传的文件的编号', (result[0].dataValues.file_list_latest).split(','));
        // 转为一个数组
        const latest_list = (result[0].dataValues.file_list_latest).split(',');
        // latest_list.forEach(el => {
        //     console.log('el:::::::',el);
        //     const res = await ctx.model.Stufile.findOne({
        //         where:{
        //             id:el
        //         }
        //     });
        //     console.log(res);
        //     // result_file.push(res);
        // });
        // 最新上传的文件
        const result2 = await ctx.model.Stufile.findAll({
            where: {
                id: {
                    [Op.in]: latest_list
                }
            },
        });
        // console.log('result2',result2);
        return result2;
    }
    // 评阅老师获取数据需要审核的数据
    async reviewFile(keywords, pageIndex, pageSize) {
        const { ctx } = this;
        // 从选题表中获取学生的数据
        const result = await ctx.model.Topic.findAndCountAll({
            where: {
                title: {
                    [Op.like]: '%' + keywords + '%'
                }
            },
            include:[
                {
                    model:ctx.model.Teacher,
                    as:'teacher'
                },
                {
                    model:ctx.model.Student,
                    as:'student'
                }
            ],
            offset: (pageIndex - 1) * pageSize,
            limit: pageSize
        });
        return result;
    }
}
module.exports = StufileService;
